import {TButton, TPageControl, TPanel} from "../../hcl/StdCtrls.js";
import {TAlign, TVerticalAlign} from "../../hcl/Controls.js";
import {TColor, THCCanvas} from "../../hcl/Graphics.js";
import {theme} from "../../hcl/theme.js";
import {hcl} from "../../hcl/HCL.js";
import {FilePanel} from "./FilePanel.js";
import {StartPanel} from "./StartPanel.js";
import {InsertPanel} from "./InsertPanel.js";

export class EmrTabControl extends TPageControl{
    constructor(options) {
        super();
        //整体宽度
        this.width =1920;

        //tab选项的高度
        this._headerHeight = 46;

        //每一个tab项宽度
        this._itemWidth = 62;

        //tab项内容对齐方式 目前只支持居中
        this._itemAlign = TVerticalAlign.Center;

        //tab字体大小
        this._itemFontSize = 11;

        //tab项选中时背景颜色
        this._itemActiveBackGroundColor = TColor.rgbaToColor(255, 255, 255);

        //tab项字体颜色
        this._itemColor =  TColor.rgbaToColor(255, 255, 255);

        //tab项选中时边框颜色
        this._itemActiveBorderColor = TColor.rgbaToColor(255, 255, 255);

        //当前控件对齐方式 左对齐
        this.align=TAlign.Left;

        //是否显示关闭按钮
        this.showCloseButton = false;

        //当前控件颜色
        this.color =  TColor.rgbaToColor(2, 119, 189);


        this.setPanel_("文件");

        this.setPanel_("开始");

        this.setPanel_("插入");

        this.setPanel_("布局");

        this.setPanel_("引用");

        this.setPanel_("协作");

        this.setPanel_("数据");

        //选中第一行
        this._activePageIndex = 0;


    }

    setPanel_(text){
        var tPage = this.addPage(text);

        switch (text){
            case "文件":
                var panel = new FilePanel({
                    top:this.top+this._headerHeight-this._paddingTop-1,
                    width:this.width,
                    color:this._itemActiveBorderColor,
                    height:this.height
                });
                tPage.addControl(panel);
                break;
            case "开始":
                var panel = new StartPanel({
                    top:this.top+this._headerHeight-this._paddingTop-1,
                    width:this.width,
                    color:this._itemActiveBorderColor,
                    height:this.height
                });
                tPage.addControl(panel);
                break;
            case "插入":
                var panel = new InsertPanel({
                    top:this.top+this._headerHeight-this._paddingTop-1,
                    width:this.width,
                    color:this._itemActiveBorderColor,
                    height:this.height
                });
                tPage.addControl(panel);
                break;
            default:

                break
        }


    }

    //tab项重新渲染
    _formatHeader(page) {
        let vW = this.vW_(page.text);

        if(this._itemWidth){
            vW=this._itemWidth;
        }

        page._headerRect_.resetBounds(0, 0, vW + theme.marginSpaceDouble, this._headerHeight);
        let vLeft = 0, vPage;
        for (let i = 0; i < this.controls.count; i++) {
            vPage = this.controls[i];
            vPage._headerRect_.resetBounds(vLeft, 0, vPage._headerRect_.width, vPage._headerRect_.height);
            vLeft += vPage._headerRect_.width;
        }
    }

    //关闭页签事件
    closePage(pageIndex) {
        if(!this.showCloseButton){
            return;
        }
       super.closePage(pageIndex);
    }

    //获取文字宽度
    vW_(text){
        let vW = THCCanvas.textWidth(THCCanvas.DefaultFont, text);
        if (this._showCloseButton)
            vW += theme.iconWidth;
        return vW;
    }

    //鼠标移入事件
    doMouseMove_(e) {
        if(!this.showCloseButton){
            return;
        }
        super.doMouseMove_(e);
    }


    //渲染
    doPaintPageHeader(hclCanvas) {
        let vPage;
        hclCanvas.font.assign(THCCanvas.DefaultFont);  //.color = theme.textColor;

        if(this._itemFontSize){
            hclCanvas.font.size = this._itemFontSize;
        }
        for (let i = 0; i < this.controls.count; i++) {
            vPage = this.controls[i];

            var vX = vPage._headerRect_.left + theme.marginSpace;
            var vY = vPage._headerRect_.top + theme.marginSpace;
            if(this._itemAlign ==TVerticalAlign.Center){
                var vW = this.vW_(vPage.text);
                vX =  vPage._headerRect_.left +Math.max(0, (vPage._headerRect_.width - vW) / 2);

                vY = Math.max(0, (this._headerHeight - hclCanvas.font.height) / 2);
            }


            if (i == this._activePageIndex&&this._itemActiveBackGroundColor) {
                    hclCanvas.brush.color = this._itemActiveBackGroundColor;
                    hclCanvas.fillBounds(vPage._headerRect_.left, vPage._headerRect_.top, vPage._headerRect_.width, vPage._headerRect_.height);

                    //底部
                 //hclCanvas.fillBounds(vPage.left,vPage._headerRect_.top+ vPage._headerRect_.height, vPage.width, this.height-vPage._headerRect_.height);
                hclCanvas.font.color = this.color;
            }else{
                if(this._itemColor){
                    hclCanvas.font.color = this._itemColor;

                }
            }

            //设置文字
            hclCanvas.textOut(vX, vY, vPage.text);

            //图标
            if (i == this._hotBtnPageIndex) {
                hclCanvas.brush.color = theme.backgroundHotColor;
                hclCanvas.fillBounds(vPage._headerRect_.right - theme.iconWidth, vPage._headerRect_.top, theme.iconWidth, theme.iconWidth);
            }

            if (i == this._activePageIndex) {
                if (this._showCloseButton)
                    hclCanvas.drawImage(vPage._headerRect_.right - theme.iconWidth, vPage._headerRect_.top + 2, theme.closeImage);

                if(this._itemActiveBorderColor){
                    hclCanvas.pen.color = this._itemActiveBorderColor;
                }else{
                    hclCanvas.pen.color = theme.borderColor;
                }
                // hclCanvas.brush.color = theme.backgroundHotColor;
                hclCanvas.pen.width = theme.borderWidth;
                hclCanvas.beginPath();
                hclCanvas.moveTo(0, this._headerHeight);
                hclCanvas.lineTo(vPage._headerRect_.left + 1, this._headerHeight);
                hclCanvas.lineTo(vPage._headerRect_.left + 1, 0);
                hclCanvas.lineTo(vPage._headerRect_.right, 0);
                hclCanvas.lineTo(vPage._headerRect_.right, this._headerHeight);
                hclCanvas.lineTo(this.width, this._headerHeight);
                hclCanvas.paintPath();
            } else if (this._showCloseButton && (i == this._hotHeaderIndex))
                hclCanvas.drawImage(vPage._headerRect_.right - theme.iconWidth, vPage._headerRect_.top + 2, theme.closeImage);
        }
    }
}